package com.itheima.reggie.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
//import com.itheima.reggie.utils.AliyunSmsUtil;

import com.itheima.reggie.utils.SMSUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.javassist.compiler.ast.Variable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Struct;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    //获取验证码
    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody User user, HttpSession session) {
        //    获取手机号
        String phone = user.getPhone();
        //随机生成六位数验证码
        if (StringUtils.isNotEmpty(phone)) {
            String code = RandomUtil.randomNumbers(6);
            //int code1 = Integer.parseInt(code);
            log.info("验证码：{}", code);
            //    发送验证码
            //SMSUtils.sendMessage("瑞吉外卖", "", phone, code);
            //AliyunSmsUtil.sendSms(phone,code1);
            //session存入验证码，手机号作为key

            HashMap<Object, Object> map1 = new HashMap<>();
            map1.put("user", user);
            map1.put("code", code);
            session.setAttribute("loginendMsg", map1);
        }

        return R.success("短信发送失败");
    }

    //    登录校验
    @PostMapping("/login")
    public R login(@RequestBody Map map, HttpSession session) {
        //获取手机号和验证码
        String phone = (String) map.get("phone");
        String code = (String) map.get("code");
        //判断手机号码和验证码为空
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
            return R.error("手机号或者验证码不能为空");
        }
        //判断手机号用户是否存在,不存在就保存
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getPhone, phone);
        User one = userService.getOne(lqw);
        if (one == null) {
            User user = new User();
            user.setPhone(phone);
            userService.save(user);

        }
      /*  else {
            return R.error("手机号未注册");
        }*/
        //判断验证码是否正确
        HashMap<Object, Object> loginUser = (HashMap<Object, Object>) session.getAttribute("loginendMsg");
        String code1 = (String) loginUser.get("code");
        if (!code.equals(code1)) {
            return R.error("验证码错误");
        }
        session.removeAttribute("loginendMsg");

        LambdaQueryWrapper<User> lqwq = new LambdaQueryWrapper<>();
        lqwq.eq(User::getPhone, phone);
        User one1 = userService.getOne(lqwq);
        session.setAttribute("loginUser", one1);
        //登陆成功
        return R.success("登录成功");
    }

    //退出登录
    @PostMapping("/loginout")
    public R logout(HttpSession session) {
        session.removeAttribute("loginUser");
        return R.success("退出成功");
    }

}
